//  
//  dialog.cs
//  
//  Author:
//       Brian L. Crawford <kg4mfq@gmail.com>
// 
//  Copyright (c) 2011 Brian L. Crawford
// 
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
// 
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
// 
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
using System;
using System.Windows.Forms;
using System.Threading;
using System.Timers;
using System.ComponentModel;
using System.IO;
using System.Drawing;

namespace FileCompare
{

    public class dialog : Form
    {
        bool checkedflag, f1flag, f2flag;
        string path;
        string search = "Model";
        Container components;
        TabControl tabcontrol1;

        public dialog()
        {
            this.Text = "File Utilities";
            this.Size = new System.Drawing.Size(780, 656);
            this.CenterToScreen();
            // create tool strip panel
            ToolStripPanel tsp = new ToolStripPanel();
            // dock edge
            tsp.Dock = DockStyle.Top;
            //menu strip
            MenuStrip ms = new MenuStrip();
            ToolStripMenuItem filemenu = new ToolStripMenuItem("File");
            ToolStripMenuItem savereport = new ToolStripMenuItem("Save Report", null, new EventHandler(savereport_click));
            ToolStripMenuItem emailreport = new ToolStripMenuItem("Email Report", null, new EventHandler(emailreport_click));

            filemenu.DropDownItems.Add(emailreport);
            filemenu.DropDownItems.Add(savereport);

            ToolStripDropDownMenu dd = (ToolStripDropDownMenu)filemenu.DropDown;
            dd.ShowImageMargin = true;
            dd.ShowCheckMargin = true;
            //actions menu
            ToolStripMenuItem actionmenu = new ToolStripMenuItem("Actions");
            ToolStripMenuItem comparefiles = new ToolStripMenuItem("Compare Files", null, new EventHandler(comparefiles_click));
            ToolStripMenuItem renamefiles = new ToolStripMenuItem("Rename Files", null, new EventHandler(renamefiles_click));
            actionmenu.DropDownItems.Add(comparefiles);
            actionmenu.DropDownItems.Add(renamefiles);

            ToolStripDropDownMenu dd1 = (ToolStripDropDownMenu)actionmenu.DropDown;
            dd1.ShowImageMargin = true;
            dd1.ShowCheckMargin = true;
            //help menu
            ToolStripMenuItem help = new ToolStripMenuItem("Help");
            ToolStripMenuItem howto = new ToolStripMenuItem("How To", null, new EventHandler(howto_click));
            ToolStripMenuItem about = new ToolStripMenuItem("About", null, new EventHandler(about_click));
            help.DropDownItems.Add(howto);
            help.DropDownItems.Add(about);

            about.Click += new EventHandler(about_click);
            howto.Click += new EventHandler(howto_click);

            ToolStripDropDownMenu dd2 = (ToolStripDropDownMenu)help.DropDown;
            dd2.ShowImageMargin = true;
            dd2.ShowCheckMargin = true;

            //Assign the ToolStripMenuItem that displays
            //the list of child forms
            ms.MdiWindowListItem = filemenu;
            //add the window toolstripmenuitem to the menustrip
            ms.Items.Add(filemenu);
            ms.Items.Add(actionmenu);
            ms.Items.Add(help);
            //dock the menustrip to the top of the form
            ms.Dock = DockStyle.Top;
            //the form mainmenustrip property determines the merge target
            this.MainMenuStrip = ms;
            //add the toolstrippanels to the form in reverse order ( draw order)
            this.Controls.Add(tsp);
            this.Controls.Add(ms);
            this.SizeGripStyle = SizeGripStyle.Show;

            InitializeComponent();
        }


        //init routine
        private void InitializeComponent()
        {
            this.components = new Container();
            this.tabcontrol1 = new TabControl();

            tabcontrol1.Size = this.ClientSize;
            tabcontrol1.Top = this.MainMenuStrip.Bottom;
            tabcontrol1.Height = tabcontrol1.Height - this.MainMenuStrip.Bottom;
            tabcontrol1.Anchor = (AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right);

            tabcontrol1.SelectedIndex = 0;
            tabcontrol1.TabIndex = 0;
            this.Controls.Add(tabcontrol1);

            //add compare class to tab 1
            comparefiles tabcompare = new comparefiles();
            tabcontrol1.Controls.Add(tabcompare);

            //add rename class to tab1
            renamefiles tabrename = new renamefiles();
            tabcontrol1.Controls.Add(tabrename);
        }


        private void howto_click(object sender, EventArgs e)
        {
            // SaveCompare();
        }

        private void about_click(object sender, EventArgs e)
        {
            MessageBox.Show(string.Format(@"{0} Version: {1}
Copyright 2010 Brian L. Crawford
Licensed under the Apache License, Version 2.0 (the License);
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an AS IS BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

This software uses Microsoft .Net framework used with permission from
Microsoft. Microsoft, .Net Framework, Visual Studio, Visual Basic, Visual C++
remain the trademark and copyright of Microsoft and are subject to their
individual licenses.", this.ProductName, this.ProductVersion));
        }

        private void comparefiles_click(object sender, EventArgs e)
        {

        }

        private void emailreport_click(object sender, EventArgs e)
        {

        }

        private void renamefiles_click(object sender, EventArgs e)
        {

        }

        private void savereport_click(object sender, EventArgs e)
        {

        }

    }

    #region Compare Tab
    //file compare tab class
    class comparefiles : TabPage
    {
        //globals
        Button folder1;
        Button folder2;
        //CheckBox subfiles;
        Button compare;
        //Button cancel;
        Button save;
        ListBox list1;
        ListBox list2;
        System.Drawing.Size s = new System.Drawing.Size(218, 52);
        Color textcolor = Color.Black;
        bool f1stat;
        bool f2stat;
        BackgroundWorker worker1;
        BackgroundWorker worker2;

        public comparefiles()
        {
            this.Text = "Compare Files";


            //folder 1 button
            folder1 = new Button();
            this.Controls.Add(folder1);
            folder1.Size = s;
            folder1.Location = new System.Drawing.Point(folder1.Width, this.Top);
            //folder1.Anchor = (AnchorStyles.Top | AnchorStyles.Left);
            folder1.TabIndex = 1;
            folder1.Text = "Pick Folder 1";
            folder1.Click += new EventHandler(folder1_Click);


            //folder 2 button
            folder2 = new Button();
            this.Controls.Add(folder2);
            folder2.Location = new System.Drawing.Point(folder1.Right + 28, this.Top);
            //folder2.Anchor = (AnchorStyles.Top);
            folder2.Size = s;
            folder2.TabIndex = 2;
            folder2.Text = "Pick Folder 2";
            folder2.Click += new EventHandler(folder2_Click);


            //compare button
            compare = new Button();
            this.Controls.Add(compare);
            compare.Size = s;
            compare.Left = PixelCenter(folder1.Right, folder2.Left) - compare.Width / 2;
            //compare.Anchor = AnchorStyles.Bottom;
            compare.Dock = DockStyle.Bottom;
            compare.Text = "Compare";
            compare.TabIndex = 3;
            compare.Click += new EventHandler(compare_Click);
            compare.Enabled = false;

            //list box1 : reference box
            list1 = new ListBox();

            list1.Location = new Point(folder1.Left, folder1.Bottom + 28);
            list1.Width = folder1.Width;
            list1.Height = 350;
            //list1.Anchor = (AnchorStyles.Top | AnchorStyles.Bottom);
            list1.SelectionMode = SelectionMode.MultiExtended;
            list1.Visible = true;
            list1.DrawMode = DrawMode.Normal;
            list1.ForeColor = textcolor;
            list1.HorizontalScrollbar = true;
            //list1.Items.Add("list1");
            this.Controls.Add(list1);

            // listbox2 : the box to compare
            list2 = new ListBox();
            this.Controls.Add(list2);
            list2.Location = new Point(folder2.Left, folder2.Bottom + 28);
            list2.Width = folder1.Width;
            list2.Height = 350;
            //list2.Anchor = (AnchorStyles.Top | AnchorStyles.Bottom);
            list2.SelectionMode = SelectionMode.MultiExtended;
            list2.Visible = true;
            list2.DrawMode = DrawMode.Normal;
            list2.ForeColor = textcolor;
            list2.HorizontalScrollbar = true;
            //list2.Items.Add("list2");

            worker1 = new BackgroundWorker();
            worker1.WorkerReportsProgress = true;
            worker1.DoWork += new DoWorkEventHandler(worker1_DoWork);
            worker1.ProgressChanged += new ProgressChangedEventHandler(worker1_ProgressChanged);
            worker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker1_RunWorkerCompleted);

            worker2 = new BackgroundWorker();
            worker2.WorkerReportsProgress = true;
            worker2.DoWork += new DoWorkEventHandler(worker2_DoWork);
            worker2.ProgressChanged += new ProgressChangedEventHandler(worker2_ProgressChanged);
            worker2.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker2_RunWorkerCompleted);
        }



        private void resetfolderstatus()
        {
            f1stat = false;
            f2stat = false;
            list1.Items.Clear();
            list2.Items.Clear();
            list2.Refresh();
            list1.Refresh();
        }

        void compare_Click(object sender, EventArgs e)
        {
            worker1.RunWorkerAsync(folder1.Text);
            worker2.RunWorkerAsync(folder2.Text);
        }

        void worker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            throw new NotImplementedException();
        }

        void worker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            throw new NotImplementedException();
        }

        void worker1_DoWork(object sender, DoWorkEventArgs e)
        {

        }

        void worker2_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            throw new NotImplementedException();
        }

        void worker2_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            throw new NotImplementedException();
        }

        void worker2_DoWork(object sender, DoWorkEventArgs e)
        {
            throw new NotImplementedException();
        }

        private int PixelCenter(int x1, int x2)
        {
            return x1 + ((x2 - x1) / 2);
        }

        private bool Collect_Subfiles(string FolderPath, BackgroundWorker Worker, DoWorkEventArgs e)
        {
            try
            {
                DirectoryInfo _directory = new DirectoryInfo(FolderPath);
                Collect_Files(_directory,ref Worker,ref e);
                object[] subfolder = _directory.GetDirectories();
                foreach (DirectoryInfo di in subfolder)
                {
                    Collect_Files(di, ref Worker, ref e);
                    Collect_Subfiles(di.FullName, Worker, e);
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        private void Collect_Files(DirectoryInfo Directory, ref BackgroundWorker worker, ref DoWorkEventArgs e)
        {
            FileInfo[] Files = Directory.GetFiles();
            foreach (FileInfo _file in Files)
            {
                worker.ReportProgress(0, _file);
            }
        }

        void folder2_Click(object sender, EventArgs e)
        {
            resetfolderstatus();
            BrowseFolder folder = new BrowseFolder();
            folder2.Text = folder.GetBrowse("Select the Folder containing files to compare against the EDDS folder", folder1.Text);

            if (folder.Result & (folder1.Text != "Pick Folder 1" && folder1.Text != ""))
            {
                compare.Enabled = true;
            }
            else
            {
                compare.Enabled = false;
            }

        }

        void folder1_Click(object sender, EventArgs e)
        {
            resetfolderstatus();
            BrowseFolder folder = new BrowseFolder();
            folder1.Text = folder.GetBrowse("Select the Folder containing files to compare against the EDDS folder", folder1.Text);

            if (folder.Result & (folder2.Text != "Pick Folder 2" && folder2.Text != ""))
            {
                compare.Enabled = true;
            }
            else
            {
                compare.Enabled = false;
            }
        }
    }
    #endregion

    #region Rename Tab
    class renamefiles : TabPage
    {
        public renamefiles()
        {
            this.Text = "Rename";
        }
    }
    #endregion
}

